<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class sbUser {

    static public function createNewUser($first_name, $last_name, $email, $phone, $password, $country, $agent, $language) {
        $user = tblUsersTable::getInstance()->findByDql("email=? and status=?", array($email, 1))->getFirst();
        if ($user) {
            return 'user exists';
        } else {
            $user = new tblUsers();
            $user->setFirstName($first_name);
            $user->setLastName($last_name);
            $user->setEmail($email);
            $user->setPassword(sha1($password));
            $user->setType(sbValues::TYPE_USER_STANDART);
            $user->setCountry($country);
            $user->setPermissions(sbValues::PERMISSIONS_STANDART);
            $user->setLanguage($language);
            $user->setCreateTime(sbValues::getCurrentTime());
            $user->setUpdateTime(sbValues::getCurrentTime());
            $user->save();

            $struc = new tblUsersStructure();
            $struc->setSubsidiaryId($user->getId());
            $struc->setMainId(sbValues::MAIN_USER);
            $struc->setUpdateTime(sbValues::getCurrentTime());
            $struc->setCreateTime(sbValues::getCurrentTime());
            $struc->save();

            return 'registration success, please check mail';
        }
    }

    static public function resetPassword($username) {
        $user = tblUsersTable::getInstance()->findByDql("email=? and status=?", array($username, 1))->getFirst();
        if ($user) {
            //create and save new password
            $pass = sbValues::generatePassword();
            $user->setPassword(sha1($pass));
            $user->setUpdateTime(sbValues::getCurrentTime());
            $user->save();
            //send email with new password
            sbValues::createNewResetEmail($username, $pass);
            return sbValues::PASSWORT_RESET_SUCCSESS;
        } else {
            return sbValues::PASSWORD_RESED_UNSUCCSESS;
        }
    }

    static public function getFullUserName($id) {
        $user = tblUsersTable::getInstance()->findByDql("id=? and status=?", array($id, 1))->getFirst();
        if ($user) {
            return $user->getFirstName() . " " . $user->getLastName();
        }
        return false;
    }

    static public function getUserData($id) {
        $user = tblUsersTable::getInstance()->findByDql("id=? and status=?", array($id, 1))->getFirst();
        if ($user) {
            return $user;
        }
        return false;
    }

    static public function userLogin($email, $pass) {
        $user = tblUsersTable::getInstance()->findByDql('email=? and password=? and status=?', array($email, sha1($pass), sbValues::STATUS_ACTIVE))->getFirst();
        if ($user) {
            $user->setUpdateTime(sbValues::getCurrentTime());
            $user->save();
            return $user;
        } else {
            return null;
        }
    }

    static public function userOrders($id, $order_id, $date_from, $date_to, $page) {
        $limit = ($page * sbValues::HOTELS_IN_ADMIN_PAGE) - (sbValues::HOTELS_IN_ADMIN_PAGE);
        if ($order_id != 0) {
            $orders = tblOrdersTable::getInstance()->findByDql('id=?', $order_id)->getFirst();
            $row = 1;
        } else if ($order_id == 0 && $date_from == 0 && $date_to == 0) {
            $orders = Doctrine_Query::create()->
                    select('u.*')->
                    from('tblUsers u')->
                    where('u.id=?', $id)->
                    offset($limit)->
                    limit(sbValues::HOTELS_IN_ADMIN_PAGE)->
                    execute();

            $rows = Doctrine_Query::create()->
                    select('COUNT(*)')->
                    from('tblUsers')->
                    where('id=?', $id)->
                    execute();

            $row = $rows[0]['COUNT'];
        } else {
            $orders = Doctrine_Query::create()->
                    select('u.*')->
                    from('tblUsers u')->
                    where('u.id=? and u.from_date>=? and u.to_date<=?', array($id, $date_from, $date_to))->
                    offset($limit)->
                    limit(sbValues::HOTELS_IN_ADMIN_PAGE)->
                    execute();

            $rows = Doctrine_Query::create()->
                    select('COUNT(*)')->
                    from('tblUsers')->
                    where('id=? and from_date>=? and to_date<=?', array($id, $date_from, $date_to))->
                    execute();

            $row = $rows[0]['COUNT'];
        }

        if ($orders) {
            return array('orders' => $orders, 'row' => $row);
        } else {
            return array('orders' => null, 'row' => null);
        }
    }

}
